/**
 * 基础信息API接口
 */

import axios from 'axios'

// 基础配置 - 统一使用vite代理配置
const API_BASE_URL = '/api'

// 创建axios实例
const apiClient = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
apiClient.interceptors.request.use(
  (config) => {
    console.log('Base API Request:', config.method?.toUpperCase(), config.url)
    return config
  },
  (error) => {
    console.error('Base Request Error:', error)
    return Promise.reject(error)
  }
)

// 响应拦截器
apiClient.interceptors.response.use(
  (response) => {
    console.log('Base API Response:', response.status, response.config.url)
    return response.data
  },
  (error) => {
    console.error('Base Response Error:', error.response?.status, error.config?.url, error.response?.data)
    return Promise.reject(error)
  }
)

// 模型信息接口类型定义
export interface ModelInfo {
  [key: string]: any
}

export interface LLMModelsResponse {
  code: number
  message: string
  data: {
    models: Record<string, ModelInfo>
    count: number
  }
}

export interface EmbeddingModelsResponse {
  code: number
  message: string
  data: {
    models: Record<string, ModelInfo>
    count: number
  }
}

export interface AgentsResponse {
  code: number
  message: string
  data: {
    agents: Record<string, string>
    count: number
  }
}

// 工具相关接口类型定义
export interface ToolOption {
  value: string
  label: string
  category_id: string
  category_name: string
  category_code: string
  tool_type: number
  description: string
  tools_count: number
  enabled_tools_count: number
}

export interface ToolCategoryOption {
  value: number
  label: string
  category_id: number
  category_name: string
  description: string
  tools_count: number
  enabled_tools_count: number
}

export interface ToolsOptionsResponse {
  code: number
  message: string
  data: {
    tools: ToolOption[]
    count: number
  }
}

export interface ToolsCategoriesResponse {
  code: number
  message: string
  data: {
    categories: ToolCategoryOption[]
    count: number
  }
}

export interface MCPOption {
  value: string
  label: string
  mcp_id: string
  mcp_name: string
  description: string
  status: number
  status_display: string
  tools_count: number
  enabled_tools_count: number
}

export interface MCPOptionsResponse {
  code: number
  message: string
  data: {
    mcp_tools: MCPOption[]
    count: number
  }
}

export interface AllModelsResponse {
  code: number
  message: string
  data: {
    llm_models: Record<string, ModelInfo>
    embedding_models: Record<string, ModelInfo>
    llm_count: number
    embedding_count: number
  }
}

export interface SystemInfoResponse {
  code: number
  message: string
  data: {
    llm_models: Record<string, ModelInfo>
    embedding_models: Record<string, ModelInfo>
    agents: Record<string, string>
    tools_count: number
    llm_count: number
    embedding_count: number
    agent_count: number
  }
}

// 基础信息相关API
export const baseApi = {
  // 获取支持的大语言模型列表
  getLLMModels: (): Promise<LLMModelsResponse> => 
    apiClient.get('/base/models/llm'),
  
  // 获取支持的向量模型列表
  getEmbeddingModels: (): Promise<EmbeddingModelsResponse> => 
    apiClient.get('/base/models/embedding'),
  
  // 获取可用的智能体类型列表
  getAvailableAgents: (): Promise<AgentsResponse> => 
    apiClient.get('/base/agents'),
  
  // 获取工具选项列表
  getToolsOptions: (): Promise<ToolsOptionsResponse> => 
    apiClient.get('/base/tools/options'),
  
  // 获取工具集选项列表
  getToolsCategories: (): Promise<ToolsCategoriesResponse> => 
    apiClient.get('/base/tools/categories'),
  
  // 获取MCP工具选项列表
  getMCPOptions: (): Promise<MCPOptionsResponse> => 
    apiClient.get('/base/mcp/options'),
  
  // 获取所有支持的模型列表
  getAllModels: (): Promise<AllModelsResponse> => 
    apiClient.get('/base/models/all'),
  
  // 获取系统信息
  getSystemInfo: (): Promise<SystemInfoResponse> => 
    apiClient.get('/base/system/info')
}

export default apiClient 